package cn.bellychang.leetcode.question700;

/**
 *
 * You are given the root of a binary search tree (BST) and an integer val.
 *
 * Find the node in the BST that the node's value equals val
 * and return the subtree rooted with that node. If such a node does not exist, return null.
 *
 * @author ChangLiang
 * @date 2021/2/8
 */
public class Solution {

    public TreeNode searchBST(TreeNode root, int val) {

        return search(root, val);
    }

    private TreeNode search(TreeNode treeNode, int val) {
        if (treeNode == null) {
            return null;
        }
        if (treeNode.val == val) {
            return treeNode;
        } else if (treeNode.val > val) {
            return search(treeNode.left, val);
        } else {
            return search(treeNode.right, val);
        }
    }
}
